package top.liuziqian.web.controller.shop.goods;




import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.liuziqian.common.annotation.Log;
import top.liuziqian.common.core.controller.BaseController;
import top.liuziqian.common.core.domain.AjaxResult;
import top.liuziqian.common.core.page.TableDataInfo;
import top.liuziqian.common.enums.BusinessType;
import top.liuziqian.shop.domain.GoodsUnit;
import top.liuziqian.shop.service.IGoodsUnitService;

import java.util.List;


@Controller
@RequestMapping("/goods/unit")
@Api("商品单位管理")
public class UnitController extends BaseController {

    private String prefix = "shop/goods/unit";

    @Autowired
    private IGoodsUnitService unitService;

    @RequiresPermissions("goods:unit:view")

    @GetMapping()
    public String unit()
    {
        return prefix + "/unit";
    }


    /**
     * 新增单位
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @GetMapping("/edit/{unitId}")
    public String edit(@PathVariable("unitId") Long unitId, ModelMap mmap)
    {
        mmap.put("unit",unitService.selectUnitById(unitId));
        return prefix + "/edit";
    }

    /**
     * 查询单位列表
     */
    @RequiresPermissions("goods:unit:list")
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation("获取商品单位")
    public TableDataInfo list(GoodsUnit unit)
    {
        startPage();
        List<GoodsUnit> list = unitService.selectUnitList(unit);
        return getDataTable(list);
    }


    /**
     * 新增单位
     */
    @RequiresPermissions("goods:unit:add")
    @Log(title = "商品单位", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated GoodsUnit unit)
    {
        unit.setCreateBy(getLoginName());
        return toAjax(unitService.insertUnit(unit));
    }

    @RequiresPermissions("goods:unit:edit")
    @Log(title = "商品单位", businessType = BusinessType.INSERT)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated GoodsUnit unit)
    {
        unit.setUpdateBy(getLoginName());
        return toAjax(unitService.updateUnit(unit));
    }

    /**
     * 删除单位
     */
    @RequiresPermissions("goods:unit:remove")
    @Log(title = "单位", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(unitService.deleteUnitByIds(ids));
    }
}

